import router from '@/router'
import store from '@/store'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']

// 全局前置路由守卫
router.beforeEach((to, from, next) => {
  // 开启进度条
  nProgress.start()
  const token = store.state.user.token
  if (token) {
    if (to.path === '/login') {
      next()
      nProgress.done()
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
      nProgress.done()
    }
  }
})

// 后置路由守卫
router.afterEach(() => {
  nProgress.done()
})
